package com.app.weighbridge;



import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * User: lanyer
 * Date: 13-6-28
 * Time: 下午4:22
 * To change this template use File | Settings | File Templates.
 */
public class WUI extends Applet {
//    public static CommPort commPort;
Label l= new Label();
    @Override
    public void init() {

        this.setName("COM端口");

    }



    private CommPort commPort;
    private InputStream in;



    /**
     *
     * @param comName
     * @param p1
     * @param p2  数据位
     * @param p3 比特率
     * @param p4
     */
   public void openComm(String comName,int p1,int p2,int p3,int p4){
       if (commPort == null) {

           CommPortIdentifier portId = null;
           try {
               portId = CommPortIdentifier
                       .getPortIdentifier(comName);
               commPort=portId.open(comName, 2000);
           } catch (Exception e) {
               e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
           }

       }


       if (commPort instanceof SerialPort) {
           SerialPort serialPort = (SerialPort) commPort;
           try {
               serialPort.setSerialPortParams(p1, p2,p3, p4);
           } catch (UnsupportedCommOperationException e) {
               e.printStackTrace();
           }
           try {
               in = serialPort.getInputStream();
           } catch (IOException e) {
               e.printStackTrace();
           }
           // start();
       }
  }





    @Override
    public void start() {
        byte[] buffer = new byte[1024];

        int len = -1;
        StringBuffer data = new StringBuffer();


        try {
            while ((len = this.in.read(buffer)) > -1) {

                try {

                    data.append(new String(buffer, 0, len));// .substring(3, 8);

                    if(data.length()-data.lastIndexOf("\2")==16){




                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
